package com.clkj.cldisabledmanclient.mvp.myrecoverorg;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyRecoverOrgDetail;
import com.clkj.cldisabledmanclient.bean.RecoverManager;
import com.clkj.cldisabledmanclient.eventbus.StarEvent;
import com.clkj.cldisabledmanclient.http.HttpConfig;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MyRecoverOrgDetailActivity extends BaseActivity implements MyRecoverOrgDetailContract.View {


    private RelativeLayout mRlTitle;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private TextView mTvOrgName;
    private TextView mTvTrainTime;
    private TextView mTvRecoverType;
    private TextView mTvOrgPhone;
    private TextView mTvOrgAddress;
    private RadioGroup mRgIndexMenu;
    private RadioButton mRbJgjs;
    private RadioButton mRbKfs;
    private View mView1;
    private View mView2;
    private WebView mWvInroduce;
    private ListView mLvKfs;
    private TextView mTvStar;
    //private TextView mTvShare;
    private LinearLayout mLyKfs;
    private RelativeLayout mRlEmpty;
    private TextView mTvTipEmpty;

    private MyRecoverOrgDetailContract.Presenter mPresenter;

    private MyRecoverOrgDetail myRecoverOrgDetail;

    private String institutionId;
    private String personnelId;
    private ProgressDialog progressDialog;

    private List<RecoverManager> recoverManagerList = new ArrayList<>();
    private JayLvBaseAdapter<RecoverManager> mAdapter;
    private boolean getManagers;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_recover_org_detail);
        initTitle(null, null, "康复机构详情", true, null);
        initData();
        initView();
        assignView();

        getMyRecoverOrgDetail();
    }

    @Override
    public void initData() {

        getManagers = false;
        EventBus.getDefault().register(this);
        institutionId = getIntent().getStringExtra("institutionId");
        personnelId = getIntent().getStringExtra("personnelId");
        mPresenter = new MyRecoverOrgDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        mAdapter = new JayLvBaseAdapter<RecoverManager>(this, recoverManagerList, R.layout.item_recover_manager) {
            @Override
            public void convert(ViewHolder helper, int position, RecoverManager item) {

                /*ImageView ivKfs = helper.getView(R.id.iv_kfs);
                RequestOptions options = new RequestOptions().error(R.mipmap.pic_help_info_of_recover_org)
                        .placeholder(R.mipmap.ico_data_empty);
                Glide.with(mContext).load(item.getTea())
                        .apply(options)
                        .into(ivKfs);*/
                helper.setText(R.id.tv_name, item.getTeacherName());
                helper.setText(R.id.tv_office, item.getTeacherDepartment());
                helper.setText(R.id.tv_job, item.getTeacherPost());
                helper.setText(R.id.tv_major, item.getTeacherMajor());
            }
        };

        mLvKfs.setAdapter(mAdapter);

        mRgIndexMenu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    //机构介绍
                    case R.id.rb_jgjs:

                        mLyKfs.setVisibility(View.GONE);
                        mWvInroduce.setVisibility(View.VISIBLE);
                        mView1.setVisibility(View.VISIBLE);
                        mView2.setVisibility(View.INVISIBLE);
                        break;

                    //康复师
                    case R.id.rb_kfs:

                        mLyKfs.setVisibility(View.VISIBLE);
                        mWvInroduce.setVisibility(View.GONE);
                        mView1.setVisibility(View.INVISIBLE);
                        mView2.setVisibility(View.VISIBLE);

                        if (!getManagers) {

                            getRecoverManagers();
                        }
                        break;
                }
            }
        });


        //评分操作
        mTvStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragmentForStar dialogFragmentForStar = DialogFragmentForStar.getInstance("");
                dialogFragmentForStar.show(getSupportFragmentManager(), "DialogFragmentForStar");
            }
        });
    }

    @Override
    public void initView() {

        mRlTitle = findViewById(R.id.rl_title);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mTvOrgName = findViewById(R.id.tv_orgName);
        mTvTrainTime = findViewById(R.id.tv_trainTime);
        mTvRecoverType = findViewById(R.id.tv_recoverType);
        mTvOrgPhone = findViewById(R.id.tv_orgPhone);
        mTvOrgAddress = findViewById(R.id.tv_orgAddress);
        mRgIndexMenu = findViewById(R.id.rg_index_menu);
        mRbJgjs = findViewById(R.id.rb_jgjs);
        mRbKfs = findViewById(R.id.rb_kfs);
        mView1 = findViewById(R.id.view1);
        mView2 = findViewById(R.id.view2);
        mWvInroduce = findViewById(R.id.wv_inroduce);
        mLvKfs = findViewById(R.id.lv_kfs);
        mTvStar = findViewById(R.id.tv_star);
        //mTvShare = findViewById(R.id.tv_share);
        mRlEmpty = findViewById(R.id.rl_empty);
        mTvTipEmpty = findViewById(R.id.tv_tip_empty);
        mLyKfs = findViewById(R.id.ly_kfs);
    }

    @Override
    public void getMyRecoverOrgDetail() {

        mPresenter.getMyRecoverOrgDetail(institutionId);
    }

    @Override
    public void getRecoverManagers() {
        mPresenter.getRecoverManagers(institutionId, "1", "100");
    }

    @Override
    public void getMyRecoverOrgDetailSuccess(MyRecoverOrgDetail myRecoverOrgDetail) {

        mTvOrgName.setText(myRecoverOrgDetail.getInstitutionName());
        mTvRecoverType.setText(myRecoverOrgDetail.getInstitutionClass());
        mTvOrgPhone.setText(myRecoverOrgDetail.getInstitutionTel());
        mTvOrgAddress.setText(myRecoverOrgDetail.getInstitutionAddress());

        CommonUtil.initWebView(mWvInroduce);
        if (TextUtils.isEmpty(myRecoverOrgDetail.getInstitutionSynopsis())) {
            mWvInroduce.loadDataWithBaseURL(HttpConfig.URL_OF_INTERFACE_BASE, "暂无机构介绍数据", "text/html", "utf-8", null);

        } else {
            mWvInroduce.loadDataWithBaseURL(HttpConfig.URL_OF_INTERFACE_BASE, myRecoverOrgDetail.getInstitutionSynopsis(), "text/html", "utf-8", null);
        }
    }

    @Override
    public void getMyRecoverOrgDetailError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getRecoverManagersSuccess(List<RecoverManager> recoverManagers) {

        getManagers = true;
        recoverManagerList.clear();
        recoverManagerList.addAll(recoverManagers);
        if (recoverManagerList.size() == 0) {

            mLvKfs.setEmptyView(mRlEmpty);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getRecoverManagersError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void starOrgSuccess(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void starOrgError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(MyRecoverOrgDetailContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetScore(StarEvent starEvent) {

        String score = starEvent.getScore();

        mPresenter.starOrg(institutionId, personnelId, score);
    }


    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            //设置点击进度对话框外的区域对话框不消失
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage("正在加载...");
            progressDialog.show();
        }
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
